home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Functions as parameters
- Date: 22 Feb 1996 00:32:39 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb21173239@qcd.lanl.gov>
- References: <Dn4x09.8Hx@undergrad.math.uwaterloo.ca>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: clgonsal@undergrad.math.uwaterloo.ca's message of Wed, 21 Feb 1996 16:40:53 GMT
-
- In article <Dn4x09.8Hx@undergrad.math.uwaterloo.ca>
- clgonsal@undergrad.math.uwaterloo.ca (Carl Laurence Gonsalves) writes:
- <snip>
- I've often seen and even written code that uses function pointers like
- this:
- <snip>
- void foo( int (*func)( int ) ){
- <snip>
- void foo( int func( int ) ){
- <snip>
- I tried compiling this with both Watcom C 10.0 and GNU C 2.6.3, and it
- worked. So my question is: is this standard, or is this some weird compiler
- extension. I've never seen this syntax before. Does it mean the same thing
- as the first piece of code? The body of the two functions is identical, and
- they're called in the same way.
-
- Yes, this is standard and they mean the same thing. In a parameter
- list:
- 1) an array is treated as if it were a pointer to the base type instead.
- 2) a function is treated as if it were a pointer to a function instead.
-
- If they are identical, why is the (*func)(int) syntax so much more common?
-
- Because it is indeed a pointer (thus, sizeof(func) is allowed in both
- cases: if func were a function, it would naturally be disallowed): C
- programmers often don't like hiding behind syntactical
- conveniences. There are historical reasons as well.
-
- The func(int) syntax does seem easier to type as well as read, but I've
- never seen it before.
-
- It depends on whether you think in terms of calling the function with
- a function parameter and hence write foo(f), or you think of it as
- calling the function with a pointer to another function and write it
- as foo(&f). (A function name always decays to a pointer to function
- unless an operand of sizeof or &: so the two forms of calling are
- likewise equivalent.)
-
- Use whichever form you prefer ... but follow something consistently.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-